import { checkSongAPI } from '@/api/index'
export default {
    namespaced: true,
    actions: {
        async updateSongId(context, value) {
            // 检查歌曲是否可用
            checkSongAPI({ id: value }).then(
                res => {
                    context.commit('updataID', value);//检查成功则调用
                    context.state.isPlay = true;
                    context.state.playList.forEach((item, index) => {
                        if (item.id == context.state.id) {
                            context.commit('updateIndex', index)
                        }
                    })
                },
                error => { alert('暂无音源') }
            )
        },
        // 更新播放列表  并且找到当前歌曲的索引号
        updateListAndIndex(context, value) {
            context.commit('updatePlayList', value);
            context.state.playList.forEach((item, index) => {
                if (item.id == context.state.id) {
                    context.commit('updateIndex', index)
                }
            })
        },
        // 下一曲功能
        nextSong(context, value) {
            //是否随机
            if (!context.state.random) {
                if (context.state.index < context.state.playList.length - 1)
                    context.commit('updateIndex', context.state.index + 1);
                else context.commit('updateIndex', 0);
                context.commit('updataID', context.state.playList[context.state.index].id);
            } else {
                let index = Math.floor(Math.random() * context.state.playList.length);
                context.commit('updateIndex', index);
                context.commit('updataID', context.state.playList[context.state.index].id);
            }
        },
        // 上一曲功能
        lastSong(context, value) {
            //是否随机
            if (!context.state.random) {
                if (context.state.index == 0)
                    context.commit('updateIndex', context.state.playList.length - 1);
                else context.commit('updateIndex', context.state.index - 1);
                context.commit('updataID', context.state.playList[context.state.index].id);
            } else {
                let index = Math.floor(Math.random() * context.state.playList.length);
                context.commit('updateIndex', index);
                context.commit('updataID', context.state.playList[context.state.index].id);
            }
        },
        // 更新播放历史
        updateHistory(context, value) {
            if (context.state.playHistory.length == 0) {
                context.commit('updatePlayHistory', value);
            } else {
                let repeat = false;
                context.state.playHistory.forEach(item => {
                    if(item.id==value.id){
                       repeat = true;
                    }
                })
                if(!repeat) context.commit('updatePlayHistory',value);
            }
            localStorage.setItem('historySheet',JSON.stringify(context.state.playHistory))
        },
        // 初始化播放历史
        playHistoryByLoca(context, value){
            if(value&&value!=null)
                context.state.playHistory = [...value];
        },


    },
    mutations: {
        updataID(state, value) {
            state.id = value
        },
        updataPlay(state, value) {
            state.isPlay = value;
        },
        updataLyric(state, value) {
            state.lyric = value;
        },
        updateAudio(state, value) {
            state.audio = value;
        },
        updateSongInfo(state, value) {
            state.songInfo = value;
        },
        updatePlayList(state, value) {
            state.playList = value
        },
        updatePlayHistory(state, value) {
            state.playHistory.unshift(value)
        },
        clearPlayHistory(state, value){
            state.playHistory = [];
        },
        updateIndex(state, value) {
            state.index = value
        },
        updateRandom(state, value) {
            state.random = !state.random;
        }
    },
    state: {
        id: 0,
        isPlay: false,
        lyric: {}, //歌词
        audio: null,
        songInfo: {},
        index: 0,  //正在播放的歌曲的索引号
        playList: [],   //播放列表
        playHistory: [],  //播放历史
        random: false,  //是否为随机播放
    }
}